package cn.kgc.layui.service.impl;

import cn.kgc.layui.dao.AuthorityDao;
import cn.kgc.layui.dao.impl.AuthorityDaoImpl;
import cn.kgc.layui.entity.Authority;
import cn.kgc.layui.entity.Student;
import cn.kgc.layui.service.AuthorityService;
import cn.kgc.layui.util.LayUiResult;
import cn.kgc.layui.vo.ZtreeVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AuthorityServiceImpl implements AuthorityService {
    private AuthorityDao authorityDao=new AuthorityDaoImpl();
    @Override
    public LayUiResult<Authority> list(String authorityName, String menuUrl, String authority) {
        List<Authority> list= authorityDao.list(authorityName,menuUrl,authority);
        LayUiResult<Authority> layUiResult = LayUiResult.<Authority>builder().code(0).msg("请求成功").count(list.size()).data(list).build();
        return layUiResult;
    }

    @Override
    public int save(String authorityName, String menuUrl, int parentId, int isMenu, String menuIcon, int orderNumber, String authority) {
        int result=0;
        try {

            Authority au=Authority.builder()
                    .authorityName(authorityName)
                    .menuUrl(menuUrl)
                    .parentId(parentId)
                    .isMenu(isMenu)
                    .menuIcon(menuIcon)
                    .orderNumber(orderNumber)
                    .createTime(new Date())
                    .authority(authority)
                    .build();
            result=authorityDao.save(au);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int update(int authorityId, String authorityName, String menuUrl, int parentId, int isMenu, String menuIcon, int orderNumber, String authority) {
        int result=0;
        try {
            Authority au=Authority.builder()
                    .authorityName(authorityName)
                    .menuUrl(menuUrl)
                    .parentId(parentId)
                    .isMenu(isMenu)
                    .menuIcon(menuIcon)
                    .orderNumber(orderNumber)
                    .createTime(new Date())
                    .authority(authority)
                    .authorityId(authorityId)
                    .build();
            result=authorityDao.update(au);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int delete(String id, String ids) {
        int result=0;
        try {
            if(null!=id&&id.length()>0){
                result=authorityDao.delete(id);
            }else{
                String [] idArra= ids.split(",");
                for (String myId:idArra) {
                    result=authorityDao.delete(myId);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public LayUiResult<ZtreeVO> ztreeData(String roleId) {
        List<Integer> authIds= authorityDao.getAuthIdsByRoleId(roleId);
        List<ZtreeVO> list= authorityDao.ztreeData();
        for (ZtreeVO zvo:list) {
            //todo 需要查询已经有的权限
            if(authIds.contains(zvo.getId())){
                zvo.setChecked(true);
            }else{
                zvo.setChecked(false);
            }

        }
        LayUiResult<ZtreeVO> layUiResult = LayUiResult.<ZtreeVO>builder().code(0).msg("请求成功").count(list.size()).data(list).build();
        return layUiResult;
    }
}
